/**
 * 一些插件的封装和共用方法
 */
var QTYYComponents = function() {
	/**
	 * 获取项目路径
	 */
	var getContextPath = function() {
		var pathName = document.location.pathname;
		var index = pathName.substr(1).indexOf("/");
		var result = pathName.substr(0, index + 1);
		return result;
	}

	/**
	 * 绑定左侧菜单的点击事件
	 */
	var handleLeftMenuClick = function() {
		$(".page-sidebar-menu").off("click", "li > a").on("click", "li > a", function() {
			var $this = $(this);
			var $sideBar = $(".page-sidebar-menu");
			$sideBar.find("li").removeClass("active open");
			$sideBar.find(".selected").remove();
			$this.closest("li").addClass("active open");
			$this.find(".title").after("<span class='selected' ></span>");
			return;
		});
	}

	/**
	 * 弹出层
	 */
	var layerOpen = function(title, width, height, content) {
		$.ajax({
			type : "GET",
			cache : false,
			url : content,
			success : function(response) {
				return layer.open({
					type : 1,
					zIndex : 20,
					skin : "qiyu-custom-layer",
					title : title,
					area : [ width, height ],
					fix : false, // 不固定
					shadeClose : false,
					shade : 0.3,
					scrollbar : false,
					content : response,
					end : function() {
						aiflag = 0;
						if (g_intervalId != null) {
							window.clearInterval(g_intervalId);
						}
					}
				});
			},
		});
	}

	/**
	 * 弹出层
	 */
	var layerOpenNo = function(title, width, height, content) {
		$.ajax({
			type : "GET",
			cache : false,
			url : content,
			success : function(response) {
				return layer.open({
					type : 1,
					zIndex : 20,
					skin : "qiyu-custom-layer",
					title : title,
					area : [ width, height ],
					fix : false, // 不固定
					shadeClose : false,
					shade : 0.3,
					scrollbar : false,
					content : response,
					end : function() {
						aiflag = 0;
					}
				});
			},
		});
	}

	/**
	 * 弹出提示框
	 */
	var bootstrapSweetAlert = function(sa_title, sa_message, sa_type) {
		swal({
			title : sa_title,
			text : sa_message,
			type : sa_type
		});
	}

	var initSelect = function(url, id) {
		if (url == null || url == "") {
			// 缺一不可
			$('#' + id).selectpicker('refresh');
			$('#' + id).selectpicker('render');
		} else {
			$.ajax({
				type : "GET",
				cache : false,
				url : url + "?orgcode=" + orgcode,
				success : function(response) {
					var optArr = [];
					for (var i = 0; i < response.obj.length; i++) {
						$('#' + id).append(
							"<option value=" + response.obj[i].realValue + ">" + response.obj[i].displayValue
							+ "</option>");
					}
					// 缺一不可
					$('#' + id).selectpicker('refresh');
					$('#' + id).selectpicker('render');
				}
			});
		}
	}

	var initDateRange = function(id) {
		var start = moment().subtract(29, 'days');
		var end = moment();
		function cb(start, end) {
			$('#' + id + ' span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
		}
		$('#' + id).daterangepicker(
			{
				startDate : start,
				endDate : end,
				ranges : {
					'今天' : [ moment(), moment() ],
					'昨天' : [ moment().subtract(1, 'days'), moment().subtract(1, 'days') ],
					'最近7天' : [ moment().subtract(6, 'days'), moment() ],
					'最近30天' : [ moment().subtract(29, 'days'), moment() ],
					'本月' : [ moment().startOf('month'), moment().endOf('month') ],
					'上个月' : [ moment().subtract(1, 'month').startOf('month'),
						moment().subtract(1, 'month').endOf('month') ]
				}
			}, cb);
		cb(start, end);
	}

	return {
		getContextPath : function() {
			getContextPath();
		},
		initIndexLeftMenu : function() {
			handleLeftMenuClick();
		},
		layerOpen : function(title, width, height, content) {
			layerOpen(title, width, height, content);
		},
		layerOpenNo : function(title, width, height, content) {
			layerOpenNo(title, width, height, content);
		},
		bootstrapSweetAlert : function(sa_title, sa_message, sa_type) {
			bootstrapSweetAlert(sa_title, sa_message, sa_type);
		},
		initSelect : function(url, id) {
			initSelect(url, id);
		},
		initDateRange : function(id) {
			initDateRange(id);
		},
		saveStartStop : function(storehousecode, name, mode, type) {
			$.ajax({
				type : "GET",
				cache : false,
				url : "startstop/clickSave?orgcode=" + orgcode + "&storehousecode=" + storehousecode + "&name=" + name + "&mode=" + mode + "&type=" + type,
				success : function(response) {
					if (mode == 1) {
						bootstrapSweetAlert("", "启动成功", "info");
					} else {
						bootstrapSweetAlert("", "停止成功", "info");
					}

				}
			});
		}
	}
}();